<?php
class KhoahocController extends Vnf_Controller_Action {
    function init() {

        $this->view->maximizeWin = true; // maximize window after popup
        $this->view->winWidth = 640; // default width of window
        $this->view->winHeight = 480; // default height of window
        parent::init();

        $this->setLayout('content');

        $this->view->translate = new Zend_Translate('tmx', BASE_DIR . '/translate/admin.tmx', 'en');

        $this->view->currentController = $this->_request->getControllerName();
        $this->view->objName = "Khoahoc"; // class name of Model
        $objName = $this->view->objName;
        $this->view->objModel = $obj = new $objName();

        $this->view->pk = $obj->getPrimaryKey();
    }

    public function indexAction() {
    }

    public function listAction() {
        $obj = $this->view->objModel;

        $records = $obj->fetchAll();
        $records = $records->toArray();

        $total = count($records);

        $results = array(
            'total' => $total,
            'results' => $records
        );

        echo Zend_Json::encode($results);die;
    }

    public function listAllAction() {
        $obj = $this->view->objModel;

        $records = $obj->fetchAll();
        $records = $records->toArray();

        $total = count($records);

        $results = array(
            'total' => $total,
            'results' => $records
        );

        echo Zend_Json::encode($results);die;
    }

    public function deleteAction() {
        $id = (int)$this->getRequest()->getParam('id', -1);
        $result = array();
        if($id > 0) {
            $obj = $this->view->objModel;
            $pk = $obj->getPrimaryKey();

            $obj->delete("$pk = '$id'");
            $result['success'] = true;
        }

        $strResult = Zend_Json::encode($result);
        echo $strResult;die;
    }

    public function saveAction() {
        $pk = $this->view->pk;

        $id = (int)$this->getRequest()->getParam($pk, -1);

        $params = $this->_request->getParams();
        $data = array(
            'khoahoc_name' => @$params['khoahoc_name'],
            'khoahoc_summary' => @$params['khoahoc_summary'],
            'khoahoc_info' => @$params['khoahoc_info'],
            'khoahoc_active' => @$params['khoahoc_active'],
            'khoahoc_datecreate' => @$params['khoahoc_datecreate'],
            'khoahoc_file' => @$params['khoahoc_file'],
        );

        $obj = $this->view->objModel;
        $result = array();
        $result['success'] = false;
        if ($id > 0) { // update
            try {
                $uploadPath = UPLOAD_PATH . "/" . $this->_request->getControllerName();
                $fieldUpload = "khoahoc_file";
                $newImageName = '';
                if(@$params['khoahoc_file_delete'] != 1) {
                    try {
                        if(Zend_Upload::isFileUpload($fieldUpload)) {
                            if(!is_dir($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . $uploadPath))
                            mkdir($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . $uploadPath, 0777);
                            chmod($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . $uploadPath, 0777);
                            $newImageName = time(). '_' . rand();
                            $objImageUpload = new Zend_Upload($fieldUpload, $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . $uploadPath);
                            echo "/*" . $objImageUpload->getName() . "*/";
                            $objImageUpload->setName($newImageName);
                            $objImageUpload->upload();
                            $newImageName = $uploadPath . "/" . $newImageName . "." .$objImageUpload->getFileExtention();
                        } else {
                            $where = "$pk = '$id'";
                            $oldRecord = $obj->fetchRow($where);
                            @$newImageName = $oldRecord->$fieldUpload;
                        }
                    } catch (Zend_Upload_Exception $ex){ }
                }
                $data[$fieldUpload] = $newImageName;
                $obj->update($data, "$pk = '$id'");
                $result['success'] = true;
                $result['message'] = $this->view->translate->_("Save successfull.");
            } catch (Exception $ex){
                $result['success'] = false;
                $result['message'] = $this->view->translate->_("Can't save to DB.") . "<br>" . $ex->getMessage();
            }
        } else { // add
            try {
                $uploadPath = UPLOAD_PATH . "/" . $this->_request->getControllerName();
                $fieldUpload = 'khoahoc_file';
                $newImageName = '';
                try {
                    if(Zend_Upload::isFileUpload($fieldUpload)) {
                        if(!is_dir($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . $uploadPath))
                        mkdir($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . $uploadPath, 0777);
                        chmod($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . $uploadPath, 0777);

                        $newImageName = time(). '_' . rand();
                        $objImageUpload = new Zend_Upload($fieldUpload, $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . $uploadPath);
                        $objImageUpload->setName($newImageName);
                        $objImageUpload->upload();
                        $saveImgName = $newImageName . "." .$objImageUpload->getFileExtention();
                        $newImageName = $uploadPath . "/" . $newImageName . "." .$objImageUpload->getFileExtention();
                    }
                    $data[$fieldUpload] = $newImageName;
                    $obj->insert($data);
                    $result['success'] = true;
                    $result['message'] = $this->view->translate->_("Insert successfull.");
                } catch (Zend_Upload_Exception $ex){
                    $result['success'] = true;
                    $result['message'] = $this->view->translate->_("Insert successfull.");
                }
            } catch(Exception $ex) {
                $result['message'] = $this->view->translate->_("Can't insert to DB") . "<br>" . $ex->getMessage();
            }
        }
        $strResult = Zend_Json::encode($result);
        echo $strResult;die;
    }
}